package com.portal.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置轮播图等静态资源访问路径
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("classpath:/static/uploads/")
                .addResourceLocations("file:uploads/")
                .addResourceLocations("file:src/main/resources/static/uploads/")
                .setCachePeriod(3600); // 设置缓存时间
                
        // 添加调试日志
        System.out.println("静态资源目录配置完成");
        System.out.println("当前工作目录: " + System.getProperty("user.dir"));
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")  // 允许所有来源
                .allowedMethods("*")         // 允许所有方法
                .allowedHeaders("*")         // 允许所有头
                .allowCredentials(true)      // 允许携带认证信息
                .maxAge(3600);              // 预检请求的有效期
                
        System.out.println("CORS配置完成");
    }
} 